package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.example.test.dialogfactory.MainActivity51.Data;
import easier.recycler.DataViewHolder;
import easier.recycler.MultiTypeAdapter;
import easier.recycler.TypeViewHolder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity57 extends AppCompatActivity {

    private static final String TAG = "MainActivity57";

    private Button mTest1;
    private Button mTest2;
    private RecyclerView mRecycler;
    private List< Data > mData;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity57.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main57 );
        initView();
    }

    private void initView() {

        mTest1 = (Button) findViewById( R.id.test1 );
        mTest2 = (Button) findViewById( R.id.test2 );
        mRecycler = (RecyclerView) findViewById( R.id.recycler );
        MultiTypeAdapter adapter = new MultiTypeAdapter();
        mRecycler.setAdapter( adapter );

        mRecycler.setLayoutManager( new LinearLayoutManager( this ) );
        mTest1.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mData = new ArrayList<>();
                for ( int i = 0; i < 5; i++ ) {
                    Data e = new Data();
                    e.id = i;
                    e.name = "name:" + i;
                    e.description = "数据描述: " + i;
                    e.stargazers_count = i * 10;
                    mData.add( e );
                }

                List< TypeViewHolder > typeViewHolders = new ArrayList<>();
                for ( Data item : mData ) {
                    typeViewHolders.add( new Holder( item ) );
                }

                adapter.updateViewHolders( typeViewHolders );
            }
        } );

        mTest2.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mData = new ArrayList<>();
                for ( int i = 0; i < 5; i++ ) {
                    int j = 5 - i;
                    Data e = new Data();
                    e.id = j;
                    e.name = "name:" + j;
                    e.description = "数据描述: " + j;
                    e.stargazers_count = j * 10;
                    mData.add( e );
                }

                List< TypeViewHolder > typeViewHolders = new ArrayList<>();
                for ( Data item : mData ) {
                    typeViewHolders.add( new Holder( item ) );
                }

                adapter.updateViewHolders( typeViewHolders );
            }
        } );

        findViewById( R.id.test3 ).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                if ( mData != null ) {
                    Data data = mData.get( 1 );
                    data.name = "new update" + data.name;
                    data.description = "new update" + data.description;

                    adapter.notifyItemChanged( 1 );
                }
            }
        } );

        findViewById( R.id.test4 ).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                if ( mData != null ) {
                    Data data = mData.get( 2 );
                    data.name = "new update 2" + data.name;
                    data.description = "new update 2" + data.description;

                    adapter.notifyItemChanged( 2 );
                }
            }
        } );
    }

    public class Holder extends DataViewHolder< Data > {

        private final OnClickListener mClick = new OnClickListener() {
            @Override
            public void onClick( View v ) {

                Data element = getElement();
                element.name = "self" + element.name;
                element.description = "self" + element.description;

                notifyItemChanged( mRecycler, v );
            }
        };

        public Holder( Data data ) {

            super( R.layout.activity_main51_item, data );
        }

        @Override
        protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

            super.onBind( viewHolder, root, position, type, payloads );

            Log.d( TAG, "onBind: " + viewHolder );
            Data item = getElement();

            TextView mNameText;
            TextView mDescriptionText;
            TextView mStarCountText;

            mNameText = (TextView) root.findViewById( R.id.name_text );
            mDescriptionText = (TextView) root.findViewById( R.id.description_text );
            mStarCountText = (TextView) root.findViewById( R.id.star_count_text );

            mNameText.setText( item.name );
            mDescriptionText.setText( item.description );
            mStarCountText.setText( String.format( "%d", item.stargazers_count ) );

            root.setOnClickListener( mClick );
        }

        @Override
        public boolean itemEquals( @Nullable TypeViewHolder obj ) {

            if ( obj instanceof Holder ) {
                Data element = ((Holder) obj).getElement();
                return Objects.equals( element.id, getElement().id );
            } else {
                return false;
            }
        }

        @Override
        public boolean contentEquals( @Nullable TypeViewHolder obj ) {

            if ( obj instanceof Holder ) {
                Data element = ((Holder) obj).getElement();
                return Objects.equals( element, getElement() );
            } else {
                return false;
            }
        }
    }
}